package com.ciwong.epaper.modules.reciteWords.widget;

import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.PopupWindow;

/**
 * Created by keepon on 2018/9/17.
 */
public class FlingTestView extends PopupWindow {
	private static final String TAG = "FlingTestView";
	private GestureDetector mGestureDetector;

	public FlingTestView(int width, int height) {
		super(width, height);
	}

	public FlingTestView(View contentView, int width, int height) {
		super(contentView, width, height);
	}

	public FlingTestView(View contentView, int width, int height, boolean focusable) {
		super(contentView, width, height, focusable);
		init();
	}

	private void init() {
		mGestureDetector = new GestureDetector(new gestureListener());
		setTouchInterceptor(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				 mGestureDetector.onTouchEvent(event);
				return false;
			}
		});
	}




	private class gestureListener implements GestureDetector.OnGestureListener{

		// 用户轻触触摸屏，由1个MotionEvent ACTION_DOWN触发
		public boolean onDown(MotionEvent e) {
			return false;
		}


		public void onShowPress(MotionEvent e) {
			Log.i("MyGesture", "onShowPress");
		}


		public boolean onSingleTapUp(MotionEvent e) {
			return true;
		}


		public boolean onScroll(MotionEvent e1, MotionEvent e2,
		                        float distanceX, float distanceY) {
			return true;
		}

		public void onLongPress(MotionEvent e) {
		}

		// 用户按下触摸屏、快速移动后松开，由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE, 1个ACTION_UP触发
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
		                       float velocityY) {
			Log.e(TAG, "onFling: " );
			if(mOnFlingUpListener!=null){
				mOnFlingUpListener.onFlingUp();
			}
			return true;
		}
	};

	public void setOnFlingUpListener(OnFlingUpListener onFlingUpListener) {
		mOnFlingUpListener = onFlingUpListener;
	}

	OnFlingUpListener mOnFlingUpListener;
	public    interface OnFlingUpListener{
		void  onFlingUp();
	}

}
